import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/model/task_detail_model.dart';
import 'package:yt_devops/model/task_tail_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/enums.dart';

class RepairTaskDetailModel extends TaskDetailModel<RepairTaskStatusType> {
  @override
  RepairTaskStatusType? status;

  String? siteName;

  int? createUser;

  RepairTaskDetailModel(
      {super.id,
      super.type,
      super.name,
      super.address,
      super.content,
      super.handlerBy,
      super.orgId,
      super.phone,
      super.timeOfAppointment,
      super.userId,
      super.userName,
      super.createName,
      super.handlerName,
      super.orgName,
      super.descPicUrl,
      super.processingResults,
      super.site,
      super.siteId,
      super.createTime,
      super.closeTime,
      super.endTime,
      super.email,
      super.remark,
      this.status,
      super.tails,
      this.siteName,
      this.createUser});

  factory RepairTaskDetailModel.fromJson(Map<String, dynamic> json) =>
      RepairTaskDetailModel(
        id: json['id'],
        name: json['name'],
        address: json['address'],
        content: json['content'],
        handlerBy: json['handlerBy'],
        orgId: json['orgId'],
        phone: json['phone'],
        timeOfAppointment: json['timeOfAppointment'],
        userId: json['userId'],
        orgName: json['orgName'],
        handlerName: json['handlerName'],
        userName: json['userName'],
        createName: json['createName'],
        descPicUrl: json['descPicUrl'],
        processingResults: json['processingResults'],
        endTime: json['endTime'],
        site: json['site'],
        siteId: json['siteId'],
        siteName: json['siteName'],
        remark: json['remark'],
        createUser: json['createUser'],
        tails: List.of(
          json['tails'],
        ).map((e) => TaskTailModel.fromJson(e)).toList(),
        status:
            $enumDecodeNullable($RepairTaskStatusTypeEnumMap, json['status']),
        type: $enumDecodeNullable($TaskTypeEnumMap, json['type']),
      );

  //基础信息
  List<InfoItem> get baseInfos {
    return [
      InfoItem(ResKey.workOrderCode.tr, id?.toString() ?? ''),
      InfoItem(ResKey.siteName.tr, siteName ?? ''),
      InfoItem(ResKey.failureTitle.tr, name ?? ''),
      InfoItem(ResKey.faultDescription.tr, content ?? ''),
    ];
  }

  //维修信息
  List<InfoItem> get repairInfos {
    return [
      InfoItem(ResKey.installManu.tr, orgName ?? ''),
      InfoItem(ResKey.maintainer.tr, handlerName ?? ''),
      InfoItem(ResKey.customerName.tr, userName ?? ''),
      InfoItem(ResKey.contactNumber.tr, phone ?? ''),
      InfoItem(ResKey.appointmentTime.tr, timeOfAppointment ?? ''),
      InfoItem(ResKey.customerAddress.tr, address ?? '',
          type: InfoType.address),
      InfoItem(ResKey.siteBaseInfoRemark.tr, remark ?? ''),
      InfoItem(ResKey.imageAttachment.tr, pics(), type: InfoType.img)
    ];
  }

  // 附件
  String pics() {
    String pics = descPicUrl ?? '';
    // if (tails != null) {
    //   tails!.forEach((element) {
    //     if (element.picture != null) {
    //       pics = descPicUrl! + ',' + element.picture!;
    //     }
    //   });
    // }

    return pics;
  }

  //进度信息
  // 维护工单       create("创建维护工单",0), //创建工单事件
// receive("接受维护工单",1), //接收工单事件
// refuse("拒绝维护工单",3), //拒接工单事件
// modify("修改工单", 4),  //修改工单事件
// finish("完成维护工单",2);  //完成工单事件
  List<TaskTailModel> get tasilInfos {
    var taillList = [
      TaskTailModel(name: ResKey.taskCreation.tr, eventId: 0),
      TaskTailModel(name: ResKey.workOrderRecept.tr, eventId: 1),
      TaskTailModel(name: ResKey.workOrderComplete.tr, eventId: 2),
    ];
    if (tails != null) {
      var j = 0;
      for (var i = 0; i < tails!.length; i++) {
        var eventId = tails![i].eventId!;
        var index =
            taillList.indexWhere((element) => element.eventId == eventId);
        if (index > -1) {
          tails![i].name = taillList[index].name;
          taillList[index] = tails![i];
        }
      }
    }

    return taillList;
  }
}
